package com.ruoyi.web.controller.decision;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.decision.domain.*;
import com.ruoyi.decision.service.IModelService;
import com.ruoyi.decision.service.IStrategyConfigureService;
import com.ruoyi.decision.service.ITaskStrategyModelService;
import com.ruoyi.decision.service.IUserTaskStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 模型Controller
 * 
 * @author zbw
 * @date 2022-07-20
 */
@RestController
@RequestMapping("/decision/model")
public class ModelController extends BaseController
{
    @Autowired
    private IModelService modelService;

    @Autowired
    private ITaskStrategyModelService  taskStrategyModelService;

    /**
     *  决策引擎运行控制
     */
    @PreAuthorize("@ss.hasPermi('decision:model:slist')")
    @GetMapping("/cs/{priority}")
    public String cs(@PathVariable("priority") String[] priority)
    {
        String s = Arrays.toString(priority);
        String config = "{\"totalModal\":{" +
                "\"disMax\":200," +
                "\"heavyMax\":100," +
                "\"cutMax\":200," +
                "\"portMax\":250," +
                "\"portInit\":100," +
                "\"startTimeClose\":\"2022-08-18\"," +
                "\"endTimeClose\":\"2022-08-20\"," +
                "\"priority\":" + s + "," +
                "\"totalList\":[[\"2022-08-16\",170,20,0,150],[\"2022-08-17\",370,20,0,150],[\"2022-08-18\",270,20,0,0],[\"2022-08-19\",370,20,0,0],[\"2022-08-20\",470,20,0,0],[\"2022-08-21\",170,20,0,150],[\"2022-08-22\",170,20,0,150]]}," +
                "\"disList\":[[\"d1\",1,11,150],[\"d2\",200,3,190],[\"d3\",120,2,200],[\"d4\",250,8,210],[\"d5\",130,7,180]]," +
                "\"htList\":[[\"t1\",1,150],[\"t2\",200,190],[\"t3\",20,200],[\"t4\",250,210],[\"t5\",130,180]]," +
                "\"cutList\":[[\"c1\",1,11,150],[\"c2\",200,12,190],[\"c3\",120,15,200],[\"c4\",250,8,210],[\"c5\",130,7,180]]}\n" +
                "\n";
//        List<Object> list = new ArrayList<>();
////        List<Object> list1 = new ArrayList<>();
////        List<Object> list2 = new ArrayList<>();
////        list1.add("订单1");
////        list1.add(85);
////        list2.add("订单2");
////        list2.add(56);
////        list.add(list1);
////        list.add(list2);
        return config;
    }

    /**
     * 获取模型参数json详细信息
     */
    @PreAuthorize("@ss.hasPermi('decision:model:modeljson')")
    @PostMapping("/modeljson")
    public AjaxResult getInfojson(@RequestBody TaskStrategyModel taskStrategyModel)
    {
        TaskStrategyModel taskId = new TaskStrategyModel();
        taskId.setTaskId(taskStrategyModel.getTaskId());
        List<TaskStrategyModel> taskList = taskStrategyModelService.selectTaskStrategyModelList(taskId);

        if (taskList.size()>0){
            for (TaskStrategyModel configure : taskList) {
                if (configure.getStrategyId().equals(taskStrategyModel.getStrategyId())){
                    if (configure.getModelId().equals(taskStrategyModel.getModelId())){
                        String modeJson = configure.getModelJson();
                        List<MenuModel> modeJsonList = JSON.parseArray(modeJson, MenuModel.class);
                        return AjaxResult.success(modeJsonList);
                    }
                }
            }
        }
        List<MenuModel> list = modelService.selectSPlmRtsModelByModelId(taskStrategyModel.getModelId());
        return AjaxResult.success(list);
    }
    /**
     * 查询模型分类-  树形菜单
     */
    @PreAuthorize("@ss.hasPermi('decision:model:slist')")
    @GetMapping("/slist")
    public TableDataInfo slist(Model model)
    {
        List<Menu> list = modelService.treemenu(model);
        return getDataTable(list);
    }

    /**
     * 查询模型列表
     */
    @PreAuthorize("@ss.hasPermi('decision:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(Model model)
    {
        startPage();
        List<Model> list = modelService.selectSPlmRtsModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出模型列表
     */
    @PreAuthorize("@ss.hasPermi('decision:model:export')")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Model model)
    {
        List<Model> list = modelService.selectSPlmRtsModelList(model);
        ExcelUtil<Model> util = new ExcelUtil<Model>(Model.class);
        util.exportExcel(response, list, "模型数据");
    }

    /**
     * 获取模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('decision:model:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") String modelId)
    {
        return AjaxResult.success(modelService.selectSPlmRtsModelByModelId(modelId));
    }

    /**
     * 新增模型
     */
    @PreAuthorize("@ss.hasPermi('decision:model:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Model model)
    {
        return toAjax(modelService.insertSPlmRtsModel(model));
    }

    /**
     * 修改模型
     */
    @PreAuthorize("@ss.hasPermi('decision:model:edit')")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Model model)
    {
        return toAjax(modelService.updateSPlmRtsModel(model));
    }

    /**
     * 删除模型
     */
    @PreAuthorize("@ss.hasPermi('decision:model:remove')")
    @Log(title = "模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable String[] modelIds)
    {
        return toAjax(modelService.deleteSPlmRtsModelByModelIds(modelIds));
    }
}
